home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0015_PI1.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  70 lines

  1. {$N+}
  2.  
  3. Program CalcPI(input, output);
  4.  
  5. { Not the most efficient Program I've ever written.  Mostly it's quick and
  6.   dirty.  The infinite series is very effective converging very quickly.
  7.   It's much better than Pi/4 = 1 - 1/3 + 1/5 - 1/7 ... which converges
  8.   like molasses. }
  9.  
  10. {  Pi / 4 = 4 * (1/5 - 1/(3*5^3) + 1/(5*5^5) - 1/(7*5^7) + ...) -
  11.                 (1/239 - 1/(3*239^3) + 1/(5*239^5) - 1/(7*239^7) + ...) }
  12.  
  13. {* Infinite series courtesy of Machin (1680 - 1752).  I found it in my
  14.    copy of Mathematics and the Imagination by Edward Kasner and
  15.    James R. Newman (Simon and Schuster, New York 1940, p. 77)          * }
  16.  
  17. Uses
  18.   Crt;
  19.  
  20.  
  21. Var
  22.   Pi_Fourths,
  23.   Pi          : Double;
  24.   Temp        : Double;
  25.   ct          : Integer;
  26.   num         : Integer;
  27.  
  28.  
  29. Function Power(Number, Exponent : Integer) : double;
  30. Var
  31.   ct   : Integer;
  32.   temp : double;
  33.  
  34. begin
  35.   temp := 1.00;
  36.   For ct := 1 to Exponent DO
  37.     temp := temp * number;
  38.   Power := temp
  39. end;
  40.  
  41. begin
  42.   ClrScr;
  43.   ct  := 1;
  44.   num := 1;
  45.   Pi_Fourths := 0;
  46.  
  47.   While ct <  15 DO
  48.   begin
  49.     Temp := (1.0 / (Power(5, num) * num)) * 4;
  50.  
  51.     if ct MOD 2 = 1 then
  52.       Pi_Fourths := Pi_Fourths + Temp
  53.     ELSE
  54.       Pi_Fourths := Pi_Fourths - Temp;
  55.  
  56.     Temp := 1.0 / (Power(239, num) * num);
  57.  
  58.     if ct MOD 2 = 1 then
  59.       Pi_Fourths := Pi_Fourths - Temp
  60.     ELSE
  61.       Pi_Fourths := Pi_Fourths + Temp;
  62.  
  63.     ct := ct + 1;
  64.     num := num + 2;
  65.   end;
  66.  
  67.   Pi := Pi_Fourths * 4.0;
  68.   Writeln( 'PI = ', Pi);
  69. end.
  70.